/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 报审材料编辑模板
 * +----------------------------------------------------------------------
 */
layui.define(['bluebird', 'templateInit', 'jquery', 'element', 'form', 'layerPop', 'configure', 'configureCom', 'apply'], function (exports) {
    var templateInit = layui.templateInit;
    var $ = layui.$;
    var templateHtml = $('#templateId').html();
    var element = layui.element;
    var form = layui.form;
    var layerPop = layui.layerPop;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var apply = layui.apply;
    var type = configureCom.GetQueryString('type') === '1055' ? 'edit' : 'add';
    var reportMateriaEdit = {
        data: {
            type: type,//操作类型，1055编辑,其他的新增
            singleProjectId: configureCom.GetQueryString('singleProjectId') || '',//单位工程ID
            auditItemCheckIndex: -1,//当前选中的审查项下标
            fjArr:[],
            auditItemList: [
                {
                    auditItemCode: configure.AUDIT_ORGAN_TYPE.HOUSING_CONSTRUCTION.value,
                    auditItemName: configure.AUDIT_ORGAN_TYPE.HOUSING_CONSTRUCTION.name + '审查',
                    auditItemStatus: '',
                    auditItemChecked: false,
                    auditItemOrganName: '',//审图机构名称
                    auditItemOrganId: '',//审图机构ID
                    auditItemMaterials: [

                    ]
                },
                {
                    auditItemCode: configure.AUDIT_ORGAN_TYPE.FIRE_DEFENCE.value,
                    auditItemName: configure.AUDIT_ORGAN_TYPE.FIRE_DEFENCE.name + '审查',
                    auditItemStatus: '',
                    auditItemChecked: false,
                    auditItemOrganName: '',//审图机构名称
                    auditItemOrganId: '',//审图机构ID
                    auditItemMaterials: [

                    ]
                },
                {
                    auditItemCode: configure.AUDIT_ORGAN_TYPE.PEOPLE_DEFENCE.value,
                    auditItemName: configure.AUDIT_ORGAN_TYPE.PEOPLE_DEFENCE.name + '审查',
                    auditItemStatus: '',
                    auditItemChecked: false,
                    auditItemOrganName: '',//审图机构名称
                    auditItemOrganId: '',//审图机构ID
                    auditItemMaterials: [

                    ]
                }]
        },
        init: function () {
            //初始化
            if (reportMateriaEdit.data.type === 'edit') {
                //编辑模式
                apply.param.data.singleProjectId = reportMateriaEdit.data.singleProjectId;
                apply.refusedApplyDetail().then(function (res) {
                    $(res.result).each(function (index, item) {
                        $(reportMateriaEdit.data.auditItemList).each(function (index1, item1) {
                            if (item1.auditItemCode === item.acceptType) {
                                //匹配则赋值
                                item1.auditItemChecked = true;
                                item1.auditItemOrganId = item.applyOrganization.id;
                                item1.auditItemOrganName = item.applyOrganization.name;
                                switch (item.acceptStatus) {
                                    case configure.AUDIT_TYPE.NOT_ACCEPTED.key:
                                        item1.auditItemStatus = configure.AUDIT_TYPE.NOT_ACCEPTED.value;
                                        break;
                                    case configure.AUDIT_TYPE.AGREE.key:
                                        item1.auditItemStatus = configure.AUDIT_TYPE.AGREE.value;
                                        break;
                                    case configure.AUDIT_TYPE.REFUSE.key:
                                        item1.auditItemStatus = configure.AUDIT_TYPE.REFUSE.value;
                                        break;
                                }
                            }
                        });
                    });
                    reportMateriaEdit.data.auditItemList[0].auditItemChecked = true;
                    reportMateriaEdit.data.auditItemCheckIndex = 0;
                    reportMateriaEdit.data.auditItemList[1].auditItemChecked = false;
                    reportMateriaEdit.data.auditItemList[2].auditItemChecked = false;
                    reportMateriaEdit.render();
                });
            } else {
                if (reportMateriaEdit.data.auditItemCheckIndex === -1) {
                    reportMateriaEdit.data.auditItemCheckIndex = 0;//新增的时候默认选中住建，即选中第一项
                    reportMateriaEdit.data.auditItemList[0].auditItemChecked = true;//新增的时候默认选中住建，即选中第一项
                }
                reportMateriaEdit.data.auditItemList[1].auditItemChecked = false;
                reportMateriaEdit.data.auditItemList[2].auditItemChecked = false;
                reportMateriaEdit.render();
            }
            $('.attachmentList2').attr('src', configureCom.nocache(configure.ATTA_LIST_HTML + '?type=' + reportMateriaEdit.data.type + '&checkType=2' + '&dataId=' + reportMateriaEdit.data.singleProjectId)).show();
            $('.attachmentList3').attr('src', configureCom.nocache(configure.ATTA_LIST_HTML + '?type=' + reportMateriaEdit.data.type + '&checkType=3' + '&dataId=' + reportMateriaEdit.data.singleProjectId));
            $('.attachmentList4').attr('src', configureCom.nocache(configure.ATTA_LIST_HTML + '?type=' + reportMateriaEdit.data.type + '&checkType=4' + '&dataId=' + reportMateriaEdit.data.singleProjectId));
        },
        render: function () {
            templateInit.init('view', reportMateriaEdit.data, templateHtml);
            element.render('collapse');
            element.render('tab');
            form.render();
            var str = $('li.layui-this').attr('lay-id');
            reportMateriaEdit.changeIframe(str);
            $('.layui-tab-title').on('click', 'li', function () {
                var str = $(this).attr('lay-id');
                reportMateriaEdit.changeIframe(str);
            });
        },
        changeIframe: function (str) {
            var num = (parseInt(str.substr(str.length - 4, 4)) - 1000) / 10 + 1;
            $('.attaIframe').hide();
            $('.attachmentList' + num).show();
        },
        getFjList: function (i, back) {
            if (i < 3) {
                if ($('.audit'+i).attr('checked')) {
                    window.frames['attachmentList' + (i + 2)].window.getAttaList(function (result) {
                        var obj = {
                            tsAttachmentTmplId: (i + 2),
                            attachmentFileDTOList: result
                        };
                        reportMateriaEdit.data.fjArr.push(obj);
                        i++;
                        reportMateriaEdit.getFjList(i, back);
                    });
                } else {
                    i++;
                    reportMateriaEdit.getFjList(i, back);
                }
            } else {
                return back && back();
            }
        },
        eventBind: function () {
            form.on('checkbox()', function (data) {
                if (data.elem.checked) {
                    //选中
                    $(reportMateriaEdit.data.auditItemList).each(function (index, item) {
                        if (data.elem.value === item.auditItemCode) {
                            item.auditItemChecked = data.elem.checked;
                            reportMateriaEdit.data.auditItemCheckIndex = index;
                            return false;
                        }
                    });
                }
                else {
                    //取消选中
                    $(reportMateriaEdit.data.auditItemList).each(function (index, item) {
                        if (data.elem.value === item.auditItemCode && index !== 0) {
                            //住建不能取消选中
                            item.auditItemChecked = data.elem.checked;
                        }
                        if (item.auditItemChecked) {
                            reportMateriaEdit.data.auditItemCheckIndex = index;
                        }
                    });
                }
                reportMateriaEdit.render();
            });

            $(reportMateriaEdit.data.auditItemList).each(function (index, item) {
                $('form').on('click', '#organChoose' + reportMateriaEdit.data.auditItemList[index].auditItemCode, function () {
                    layerPop.param.isTop = true;
                    layerPop.param.title = '选择审图机构';
                    layerPop.param.url = configure.AUDIT_ORGAN_LIST_SEARCH_HTTP;
                    layerPop.param.saveForm = function (data) {
                        reportMateriaEdit.data.auditItemList[index].auditItemOrganId = data.selectOrganId;
                        reportMateriaEdit.data.auditItemList[index].auditItemOrganName = data.selectOrganName;
                        reportMateriaEdit.data.auditItemCheckIndex = index;
                        reportMateriaEdit.render();
                        layerPop.close();
                    };
                    layerPop.popupNoBtn();
                });
            });
            form.on('submit(*)', function () {
                configureCom.loadON();
                //获取附件信息
                reportMateriaEdit.data.fjArr = [];
                reportMateriaEdit.getFjList(0, function () {
                    var arr = reportMateriaEdit.data.fjArr;
                    reportMateriaEdit.data.dataAttachmentDTOList = JSON.stringify(arr);
                    window.parWin.saveForm(reportMateriaEdit.data);
                    return false;
                });
                return false;//阻止表单跳转
            });
        }
    };

    exports('reportMateriaEdit', reportMateriaEdit);
});


